/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tallerfinal.util.distributions;

import tallerfinal.util.distributions.factory.GeneratorsFactory;
import tallerfinal.util.enums.DistributionsName;

/**
 *
 * @author ricardo
 */
public class NormalGenerator extends Generator<Double> {
static {
        GeneratorsFactory.getInstance().registerProduct(DistributionsName.NORMAL, NormalGenerator.class);
    }
    private double media, varianza, u1, u2, z1, r, theta;

    public NormalGenerator(double media, double varianza) {
        this.media = media;
        this.varianza = varianza;
    }

    @Override
    public Double generate() {
        u1 = generator.nextDouble();
        u2 = generator.nextDouble();
        r = Math.sqrt(-2 * Math.log(u1));
        theta = 2 * Math.PI * u2;
        z1 = r * Math.cos(theta);
        return (media + z1 * Math.sqrt(varianza));
    }
}
